package com.study.sfd.medicine.controller;

import com.study.common.utils.PageResult;
import com.study.common.utils.Result;
import com.study.sfd.medicine.service.MedicineService;
import com.study.common.vo.MedicineDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * medicine(药品)
 *
 * @author 王个球
 * @email wangdajunzy@163.com
 * @date 2021-11-12 13:37:43
 */
@RestController
@Slf4j
@RequestMapping("medicine/medicine")
public class MedicineController {
  @Autowired
  private MedicineService medicineService;

  /**
   * 列表
   */
  @GetMapping("/listWithCover")
  public Result<PageResult> listWithCover(@RequestParam Map<String, Object> params,
                                          @RequestParam(required = false) List<Integer> druggistIds) {
    PageResult page = medicineService.queryPageWithCover(params, druggistIds);

    return Result.success(page);
  }

  /**
   * 信息
   */
  @GetMapping("/{id}")
  public Result<MedicineDetail> info(@PathVariable("id") Integer id) {
    MedicineDetail medicine = medicineService.getDetailById(id);

    return Result.success(medicine);
  }

  /**
   * 保存
   */
  @PostMapping()
  public Result<?> save(@RequestBody MedicineDetail medicine)
  {
    medicineService.saveWithImgs(medicine);

    return Result.success();
  }

  /**
   * 修改
   */
  @PutMapping()
  public Result<?> update(@RequestBody MedicineDetail medicine)
  {
    medicineService.updateWithImgsById(medicine);

    return Result.success();
  }

  /**
   * 删除
   */
  @DeleteMapping()
  public Result<?> delete(@RequestBody Integer[] ids) {
    List<Integer> mdcIds = Arrays.asList(ids);
    log.info("mdcIds = " + mdcIds);
    medicineService.removeWithImgsByIds(mdcIds);

    return Result.success();
  }
}
